from django.urls import reverse
from rest_framework.test import APIClient
from video.models import Videos
from django.test import TestCase
from user.models import UserProfile
from favor.models import Favor

class TestavorViewSet(TestCase):
    def setUp(self):
        self.client = APIClient()
        self.user1 = UserProfile.objects.create_user(id=1, username='testuser1', password='123456456')
        self.user2 = UserProfile.objects.create_user(id=2, username='testuser2', password='123456456')
        self.user3 = UserProfile.objects.create_user(id=3, username='testuser3', password='123456456')
        self.video1 = Videos.objects.create(vid=1, title='test1', duration=10, mup=self.user1, type = '1')
        self.video2 = Videos.objects.create(vid=2, title='test2', duration=10, mup=self.user1, type = '2')
        self.video3 = Videos.objects.create(vid=3, title='test3', duration=10, mup=self.user1, type = '2')
        self.favor1 = Favor.objects.create(fid=1, user_id=self.user1, video_id=self.video1)
        self.favor2 = Favor.objects.create(fid=2, user_id=self.user1, video_id=self.video2)
        self.favor4 = Favor.objects.create(fid=4, user_id=self.user2, video_id=self.video1)

    def test_get_favor_count_exists(self):
        url = reverse('favor-count', kwargs={'pk':1, 'user':1})
        response = self.client.get(url)
        data = response.json()
        self.assertEqual(data['favor_count'], 2)

    def test_get_favor_count_not_exists(self):
        url = reverse('favor-count', kwargs={'pk':3, 'user':1})
        response = self.client.get(url)
        data = response.json()
        self.assertEqual(data['favor_count'], 0)

    def test_get_favor_count_favored(self):
        url = reverse('favor-count', kwargs={'pk':1, 'user':1})
        response = self.client.get(url)
        data = response.json()
        self.assertEqual(data['is_favored'], True)

    def test_get_favor_count_not_favored(self):
        url = reverse('favor-count', kwargs={'pk':1, 'user':3})
        response = self.client.get(url)
        data = response.json()
        self.assertEqual(data['is_favored'], False)

    def test_get_my_favor_exists(self):
        url = reverse('myFavor', kwargs={'user':1, 'num':2, 'from':0})
        response = self.client.get(url)
        data = response.json()
        self.assertEqual(data['count'], 2)

    def test_get_my_favor_not_exists(self):
        url = reverse('myFavor', kwargs={'user':3, 'num':2, 'from':0})
        response = self.client.get(url)
        data = response.json()
        self.assertEqual(data['count'], 0)